package com.wiscamp.ninechapters.problems.infrastructure.mappers.problems;

import com.wiscamp.ninechapters.problems.infrastructure.po.problems.QuestionOptionPO;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;

/**
 * 问题选项仓储对象
 *
 * @author yifeng
 */
@Repository
public interface QuestionOptionMapper extends JpaRepository<QuestionOptionPO, Long>, JpaSpecificationExecutor<QuestionOptionPO> {
    /**
     * 通过问题编号获取选项列表
     *
     * @param questionId
     * @return
     */
    @Query(value = "select e from QuestionOptionPO e where e.questionId = :questionId order by e.optionIndex asc")
    Collection<QuestionOptionPO> findListByQuestion(long questionId);

    /**
     * 通过问题编号删除问题选项
     *
     * @param questionId
     */
    @Modifying
    @Transactional(transactionManager = "problemsTransactionManager")
    @Query(value = "delete from QuestionOptionPO e where e.questionId = :questionId")
    void deleteByQuestion(long questionId);
}